package org.visionlibrary.image.filters.effects;

import javax.media.jai.TiledImage;

import org.visionlibrary.image.acquisition.ImageFactory;
import org.visionlibrary.image.model.AbstractFilter;


public class ColorDilation extends AbstractFilter {
	protected int size = 9;

	public ColorDilation(int size) {
		super();
		this.size = size;
	}

	@Override
	public TiledImage applyFilter(TiledImage src, TiledImage dest) {
		if (null == src)
			throw new NullPointerException("Source image is null.");

		if (null == dest)
			dest = (new ImageFactory()).createCompatibleImage(src);
		int maxX = src.getWidth();
		int maxY = src.getHeight();
		int channels = src.getNumBands();

		int ApetureMin = -(size / 2);
		int ApetureMax = (size / 2);

		for (int x = 0; x < maxX; x++)
			for (int y = 0; y < maxY; y++) {
				for (int ch = 0; ch < channels; ch++) {
					int rvalue = 0;
					for (int x2 = ApetureMin; x2 < ApetureMax; ++x2) {
						int tempX = x + x2;
						if (tempX >= 0 && tempX < dest.getWidth()) {
							for (int y2 = ApetureMin; y2 < ApetureMax; ++y2) {
								int tempY = y + y2;
								if (tempY >= 0 && tempY < dest.getHeight()) {
									int pixel = src.getSample(tempX, tempY, ch);
									if (pixel > rvalue)
										rvalue = pixel;
								}
							}
						}
					}
					dest.setSample(x, y, ch, rvalue);
				}
			}

		return dest;
	}
}
